package com.ruoyi.system.service.Impl;

import com.ruoyi.common.config.imageCompres.ImageCompres;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.system.domain.fhUploadImage;
import com.ruoyi.system.service.imgUploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Service
public class imgUploadServiceImpl implements imgUploadService {

    @Autowired
    private com.ruoyi.system.mapper.fhUploadImageMapper fhUploadImageMapper;

    @Value("${ruoyi.uploadImagePath}")
    private String borrowBusinessPath;

    @Value("${ruoyi.uploadCompresImagePath}")
    private String uploadCompresImagePath;

    @Transactional
    public void uploadImage(MultipartFile[] files, HttpServletRequest request, SysUser sysUser) {
        String path = "";
        int filesCount = files.length;
        //上传图片
        try {
            if (files.length > 0) {
                fhUploadImage fhUploadImage = null;
                for (int i = 0; i < files.length; i++) {
                    fhUploadImage = new fhUploadImage();
                    fhUploadImage.setUpId(IdUtils.simpleUUID());
                    fhUploadImage.setUserId(sysUser.getUserId().toString());
                    // 其日历字段由当前日期和时间初始化：
                    Calendar calendar = Calendar.getInstance();
                    String str = DateUtils.parseDateToStr("yyyy-MM-dd HH:mm:ss", calendar.getTime());
                    fhUploadImage.setCreateTimes(str);
//                    fhUploadImage.setIp(LoginUserAddressUtils.Addressmessage(request));
                    String fileName = fhUploadImage.getUpId() + "_" + files[i].getOriginalFilename();
                    fhUploadImage.setImageName(fileName);
                    String imgDate = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
                    path = borrowBusinessPath + File.separatorChar + imgDate;
                    File dirFile = new File(path);
                    if (!dirFile.exists())
                        dirFile.mkdirs();
                    File destFile = new File(path, fileName);
                    if (!destFile.exists())
                        destFile.createNewFile();
                    files[i].transferTo(destFile);
                    fhUploadImage.setImagePath(File.separatorChar + "uploadImagePath" + File.separatorChar + new SimpleDateFormat("yyyy-MM-dd").format(new Date()) + File.separatorChar + fileName);
                    fhUploadImage.setRequestFrom(ServletUtils.checkAgentIsMobile(request.getHeader("User-Agent")));
                    fhUploadImage.setImgRemarks("图片说明//");

                    String pathImgCamepress = uploadCompresImagePath + File.separatorChar + imgDate + File.separatorChar;
                    File compressDirFile = new File(pathImgCamepress);
                    if (!compressDirFile.exists())
                        compressDirFile.mkdirs();
                    File compressDestFile = new File(pathImgCamepress, fileName);
                    if (!compressDestFile.exists())
                        compressDestFile.createNewFile();
                    /**
                     * 压缩本地图片保存到本地
                     *
                     * @param path          保存文件的路径，精确到最后一个文件夹
                     * @param localPhotoUrl 本地图片路径，精确到文件
                     * @param fileName      新的文件名
                     * @return
                     */
                    HashMap<String, String> compress = ImageCompres.localImageCompress(pathImgCamepress, "D:" + fhUploadImage.getImagePath(), fileName);
                    fhUploadImage.setImageCompressPath(compress.get("thumbnailPath"));
                    fhUploadImageMapper.insertFhUploadImage(fhUploadImage);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public List<fhUploadImage> selectImagePathList(fhUploadImage fhUploadImage) {
        return fhUploadImageMapper.selectImagePathList(fhUploadImage);
    }
}
